//package nm.aleksey.client;
//
//import java.util.Set;
//
//import javax.validation.ConstraintViolation;
//
////import nm.aleksey.client.events.EditBookEvent;
//import nm.aleksey.client.widgets.BookEditor;
//import nm.aleksey.shared.AuthorContext;
//import nm.aleksey.shared.AuthorProxy;
////import nm.aleksey.shared.BookContext;
////import nm.aleksey.shared.BookProxy;
//import nm.aleksey.shared.TableRequestFactory;
//
//import com.google.gwt.core.client.GWT;
//import com.google.gwt.event.dom.client.ClickEvent;
//import com.google.gwt.event.dom.client.KeyCodes;
//import com.google.gwt.event.dom.client.KeyUpEvent;
//import com.google.gwt.event.dom.client.KeyUpHandler;
//import com.google.gwt.uibinder.client.UiBinder;
//import com.google.gwt.uibinder.client.UiField;
//import com.google.gwt.uibinder.client.UiHandler;
//import com.google.gwt.user.client.ui.DialogBox;
//import com.google.gwt.user.client.ui.HTMLPanel;
//import com.google.gwt.user.client.ui.Label;
//import com.google.gwt.user.client.ui.ListBox;
//import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
//import com.google.web.bindery.requestfactory.shared.Receiver;
//import com.google.web.bindery.requestfactory.shared.Request;
//import com.google.web.bindery.requestfactory.shared.RequestContext;
//
//public class BookEditorWorkflow {
//  interface Binder extends UiBinder<DialogBox, BookEditorWorkflow> {
//    Binder BINDER = GWT.create(Binder.class);
//  }
//
////  interface Driver extends RequestFactoryEditorDriver<BookProxy, BookEditor> {
////  }
//
//  static void register(final ClientFactory clientFactory) {
//    clientFactory.getEventBus().addHandler(EditBookEvent.TYPE,
//        new EditBookEvent.Handler() {
//          @Override
//          public void startEdit(ListBox bookListBox, AuthorProxy author,
//              BookProxy book, RequestContext requestContext) {
//            new BookEditorWorkflow(bookListBox, clientFactory
//                .getRequestFactory(), author, book).edit(requestContext);
//          }
//        });
//  }
//
//  private final AuthorProxy author;
//
//  @UiField
//  Label authorNameLabel;
//
//  private BookProxy book;
//
//  @UiField(provided = true)
//  BookEditor bookEditor;
//
//  private final ListBox bookListBox;
//
//  @UiField
//  HTMLPanel contents;
//
//  @UiField
//  DialogBox dialog;
//
//  private final Driver editorDriver = GWT.create(Driver.class);
//
//  private final TableRequestFactory requestFactory;
//
//  public BookEditorWorkflow(ListBox bookListBox,
//      TableRequestFactory requestFactory, AuthorProxy author, BookProxy book) {
//    this.bookListBox = bookListBox;
//    this.requestFactory = requestFactory;
//    this.book = book;
//    this.author = author;
//    bookEditor = new BookEditor();
//    Binder.BINDER.createAndBindUi(this);
//    contents.addDomHandler(new KeyUpHandler() {
//      @Override
//      public void onKeyUp(KeyUpEvent event) {
//        if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
//          onCancel(null);
//        }
//      }
//    }, KeyUpEvent.getType());
//    authorNameLabel.setText(author.getName());
//  }
//
//  /**
//   * Construct and display the UI that will be used to edit the current
//   * BookProxy, using the given RequestContext to accumulate the edits.
//   */
//  private void edit(RequestContext requestContext) {
//    // editorDriver = GWT.create(Driver.class);
//    editorDriver.initialize(getRequestFactory(), bookEditor);
//
//    if (requestContext == null) {
//      fetchAndEdit();
//      return;
//    }
//
//    editorDriver.edit(book, requestContext);
//    dialog.center();
//    // GWT.log(book.getName());
//  }
//
//  private void fetchAndEdit() {
//    // The request is configured arbitrarily
//    Request<BookProxy> fetchRequest = getRequestFactory().find(book.stableId());
//
//    // Add the paths that the EditorDrives computes
//    fetchRequest.with(editorDriver.getPaths());
//
//    // We could do more with the request, but we just fire it
//    fetchRequest.to(new Receiver<BookProxy>() {
//
//      @Override
//      public void onSuccess(final BookProxy book) {
//        BookEditorWorkflow.this.book = book;
//        // Start the edit process
//        final BookContext context = getRequestFactory().bookRequest();
//        // Display the UI
//        edit(context);
//        context.save(book);
//      }
//    }).fire();
//  }
//
//  public TableRequestFactory getRequestFactory() {
//    return requestFactory;
//  }
//
//  /**
//   * Called by the cancel button when it is clicked. This method will just tear
//   * down the UI and clear the state of the workflow.
//   */
//  @UiHandler("cancel")
//  void onCancel(ClickEvent event) {
//    dialog.hide();
//  }
//
//  /**
//   * Called by the edit dialog's save button. This method will flush the
//   * contents of the UI into the BookProxy that is being edited, check for
//   * errors, and send the request to the server.
//   */
//  @UiHandler("save")
//  void onSave(ClickEvent event) {
//    // Flush the contents of the UI
//    RequestContext context = editorDriver.flush();
//
//    // Check for errors
//    if (editorDriver.hasErrors()) {
//      dialog.setText("Errors detected locally");
//      return;
//    }
//
//    // Send the request
//    context.fire(new Receiver<Void>() {
//      @Override
//      public void onConstraintViolation(Set<ConstraintViolation<?>> errors) {
//        // Otherwise, show ConstraintViolations in the UI
//        dialog.setText("Errors detected on the server");
//        editorDriver.setConstraintViolations(errors);
//      }
//
//      @Override
//      public void onSuccess(Void response) {
//        dialog.hide();
//        AuthorContext context = getRequestFactory().authorRequest();
//        context.addBook(author, book);
//        context.fire(new Receiver<Void>() {
//
//          @Override
//          public void onSuccess(Void response) {
//            String bookHtml = book.getName() + " | " + book.getIsdn();
//            bookListBox.addItem(bookHtml, book.getId());
//          }
//
//        });
//      }
//    });
//  }
//}
